/**
 *  irq.h - Interruption management.
 *
 *  Copyright (C) 2008-2010 ZhangHu
 *  All rights reserved.
 *  E-MAIL: anmnmnly@gmail.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef __IRQ_H__
#define __IRQ_H__


#include "include/types.h"

#ifndef UNIT_DEBUG
#include "include/list.h"
#include "include/mem.h"
#include "include/core.h"
#include "hw_irq.h"

#define mac_disable_irq() DisableInt()
#define mac_enable_irq() EnableInt()
#endif


// 'isr_t' describles a ISR of a interruption
typedef struct isr_desc {
    void(*pisr)(void);  // The entry of ISR.
    list_t link;        // All ISR will linked by this member.
    uint32 irq_no;     // ISR Serial number for a interruption.
}isr_t;


typedef struct isr_head {
    list_t link;
    uint32 nr;
} isr_head_t;


extern void initirq(void); // Initialize interruption management.
extern void isr_install(uint32 IntNum,     // Serial number of interruption
                                            // that vary from architecture.
                        uint32 IrqNo,      // ISR Serial number for a interruption
                        void(*phdl)(void)); // Pointer to the entry of ISR.

// 'isrInstall' has the same function as 'isr_install'.
// The difference is that 'isr_install' executes in a unprotected context.
#define isrInstall(IntNum, IrqNo, phdl) \
do{\
    mac_disable_irq();\
    isr_install(IntNum,IrqNo,phdl);\
    mac_enable_irq();\
}while(0);

void isr_uninstall(uint32 IntNum, uint32 IrqNo);
#define isrUnInstall(IntNum, IrqNo) \
do{\
    mac_disable_irq();\
    isr_uninstall(IntNum,IrqNo);\
    mac_enable_irq();\
}while(0);

extern void isrHandler(void);  // Handle all interruptions.

// isr_after_handle - this rountine should be called after leaving
//                    'isrHandler', so that some CPU registers can be clear.
//                    It should be rewrite when port Bycore to another CPU.
void isr_after_handle(uint32 isr_num);


uint32 int_nesting(void);
bool_t int_flag(void);
void int_nesting_sub(void);
void int_flag_clr(void);
void int_flag_set(void);


#endif

